package com.tio.showcase.server;

import com.tio.showcase.common.ShowcaseAsAioHandler;
import com.tio.showcase.common.ShowcasePacket;
import com.tio.showcase.common.Type;
import com.tio.showcase.packet.HeartbeatReqHandler;
import com.tio.showcase.packet.LoginReqHandler;
import com.tio.showcase.packet.LoginReqbody;
import com.tio.showcase.showcaseintf.AbsShowcaseBsHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.core.intf.AioHandler;
import org.tio.core.intf.Packet;
import org.tio.server.intf.ServerAioHandler;

import java.util.HashMap;
import java.util.Map;

/**
 * Author      : liyuemin
 * Time        : 2017/12/15 14:55
 * Description :
 */
public class ShowcaseServerAioHandler extends ShowcaseAsAioHandler implements ServerAioHandler{
    private Logger logger = LoggerFactory.getLogger(ShowcaseServerAioHandler.class);
    private static Map<Byte,AbsShowcaseBsHandler<?>> handlerMap = new HashMap<Byte,AbsShowcaseBsHandler<?>>();
    static {
        handlerMap.put(Type.LOGIN_REQ,new LoginReqHandler());
        handlerMap.put(Type.HEART_BEAT_REQ,new HeartbeatReqHandler());
    }
    @Override
    public void handler(Packet packet, ChannelContext channelContext) throws Exception {
        ShowcasePacket showcasePacket =  (ShowcasePacket) packet ;
        Byte type = showcasePacket.getType();
        AbsShowcaseBsHandler<?> showcaseBsHandler = handlerMap.get(type);
        if(showcaseBsHandler == null){
            logger.error("{},找不到处理类，type:{}",channelContext,type);
            return ;
        }
        showcaseBsHandler.handler(showcasePacket,channelContext);
    }
}
